package cn.sykj.car.controller;


import cn.sykj.car.common.ResponseBean;
import cn.sykj.car.common.StatusCode;
import cn.sykj.car.entity.OperationLog;
import cn.sykj.car.entity.Users;
import cn.sykj.car.service.IOperationLogService;
import cn.sykj.car.service.IUsersService;
import cn.sykj.car.utils.Utils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 操作日志 前端控制器
 * </p>
 *
 * @author yixu
 * @since 2018-12-21
 */
@Api(hidden = false, tags = {"操作日志 前端控制器"})
@RestController
@RequestMapping("/operation-log")
public class OperationLogController extends BaseController{

    @Resource
    IOperationLogService operationLogService;

    @Resource
    IUsersService iSysUsersService;

    @RequiresAuthentication
    @RequiresPermissions("operationLog:view")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public ResponseBean page(@RequestBody OperationLog operationLog,
                             @RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer currentPage,
                             @RequestParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize) {

        QueryWrapper condition = new QueryWrapper();
        condition.orderByDesc(OperationLog.OPERATION_DATE);
        if (Utils.notEmpty(operationLog.getOperationUserName())) {
            condition.like(OperationLog.OPERATION_USER_NAME, operationLog.getOperationUserName());
        } else {
            condition.select(
                             OperationLog.ID,
                             OperationLog.OPERATION_USER_ID,
                             OperationLog.OPERATION_USER_NAME,
                             OperationLog.OPERATION_DATE,
                             OperationLog.OPARATION_EVENT,
                             OperationLog.REMOTE_IP);
        }
        IPage<OperationLog> page = new Page<>(currentPage,pageSize);
        page = operationLogService.page(page, condition);
        return new ResponseBean(StatusCode.OK, page);
    }
}
